package com.sleepfish.activiti.controller;


import com.sleepfish.activiti.domain.VO.task.*;
import com.sleepfish.activiti.service.BpmTaskService;
import com.sleepfish.common.activiti.PageResult;
import com.sleepfish.common.core.domain.AjaxResult;
import com.sleepfish.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


@Api(tags = "管理后台 - 流程任务实例")
@RestController
@RequestMapping("/bpm/task")
@Validated
public class BpmTaskController {

    @Resource
    private BpmTaskService taskService;

    @GetMapping("todo-page")
    @ApiOperation("获取 Todo 待办任务分页")
    //@PreAuthorize("@ss.hasPermi('bpm:task:query')")
    public AjaxResult getTodoTaskPage(@Valid BpmTaskTodoPageReqVO pageVO) {
        return AjaxResult.success(taskService.getTodoTaskPage(SecurityUtils.getLoginUser().getUserId(), pageVO));
    }

    @GetMapping("done-page")
    @ApiOperation("获取 Done 已办任务分页")
    //@PreAuthorize("@ss.hasPermi('bpm:task:query')")
    public AjaxResult getTodoTaskPage(@Valid BpmTaskDonePageReqVO pageVO) {
        return AjaxResult.success(taskService.getDoneTaskPage(SecurityUtils.getLoginUser().getUserId(), pageVO));
    }

    @PutMapping("/approve")
    @ApiOperation("通过任务")
    //@PreAuthorize("@ss.hasPermi('bpm:task:update')")
    public AjaxResult approveTask(@Valid @RequestBody BpmTaskApproveReqVO reqVO) {
        taskService.approveTask(SecurityUtils.getLoginUser().getUserId(), reqVO);
        return AjaxResult.success(true);
    }

    @PutMapping("/approve/intervene")
    @ApiOperation("通过任务-干预")
    //@PreAuthorize("@ss.hasPermi('bpm:task:update')")
    public AjaxResult approveTaskIntervene(@Valid @RequestBody BpmTaskApproveReqVO reqVO) {
        taskService.approveTaskIntervene(SecurityUtils.getLoginUser().getUserId(), reqVO);
        return AjaxResult.success(true);
    }

    @PutMapping("/reject")
    @ApiOperation("不通过任务")
    //@PreAuthorize("@ss.hasPermi('bpm:task:update')")
    public AjaxResult rejectTask(@Valid @RequestBody BpmTaskRejectReqVO reqVO) {
        taskService.rejectTask(SecurityUtils.getLoginUser().getUserId(), reqVO);
        return AjaxResult.success(true);
    }

    @PutMapping("/update-assignee")
    @ApiOperation(value = "更新任务的负责人", notes = "用于【流程详情】的【转派】按钮")
    //@PreAuthorize("@ss.hasPermi('bpm:task:update')")
    public AjaxResult updateTaskAssignee(@Valid @RequestBody BpmTaskUpdateAssigneeReqVO reqVO) {
        taskService.updateTaskAssignee(SecurityUtils.getLoginUser().getUserId(), reqVO);
        return AjaxResult.success(true);
    }

    @GetMapping("/list-by-process-instance-id")
    @ApiOperation(value = "获得指定流程实例的任务列表", notes = "包括完成的、未完成的")
    @ApiImplicitParam(name = "processInstanceId", value = "流程实例的编号", required = true, dataTypeClass = String.class)
    //@PreAuthorize("@ss.hasPermi('bpm:task:query')")
    public AjaxResult getTaskListByProcessInstanceId(
            @RequestParam("processInstanceId") String processInstanceId) {
        System.out.println(processInstanceId);
        List<BpmTaskRespVO> taskListByProcessInstanceId = taskService.getTaskListByProcessInstanceId(processInstanceId);
        for (BpmTaskRespVO bpmTaskRespVO : taskListByProcessInstanceId) {
            System.out.println(bpmTaskRespVO.toString());
            System.out.println("-------");
            System.out.println(bpmTaskRespVO.getAssigneeUser().toString());
        }
        return AjaxResult.success(taskListByProcessInstanceId);
    }

}
